// ==UserScript==
// @name         mini4kToQBittorrent
// @name:en      mini4kToQBittorrent
// @name:zh      mini4kToQBittorrent
// @namespace    http://tampermonkey.net/
// @version      1.0.1
// @description:zh-cn  一键从mini4k网站添加下载链接到qbittorrent
// @author       luoyu
// @match        https://www.mini4k.com/*
// @icon         https://www.mini4k.com/themes/mini4k/favicon.ico
// @require      https://cdn.bootcdn.net/ajax/libs/jquery/1.12.4/jquery.js
// @require      https://cdn.bootcdn.net/ajax/libs/jqueryui/1.14.1/jquery-ui.min.js
// @require      https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js
// @resource      https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css
// @resource      https://cdn.bootcdn.net/ajax/libs/jqueryui/1.14.1/themes/base/jquery-ui.min.css
// @resource      https://cdn.bootcdn.net/ajax/libs/jqueryui/1.14.1/themes/base/theme.min.css
// @updateURL     https://gitee.com/wzbao/tampermonkey/raw/master/mini4k.js
// @downloadURL   https://gitee.com/wzbao/tampermonkey/raw/master/mini4k.js
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function () {
    'use strict';
    // 链接常量
    const MINI4K_DOMAIN = 'https://www.mini4k.com';
    const mini4kMovieDetailUrlPrefix = 'https://www.mini4k.com/movies/',
        mini4kTVDetailUrlPrefix = 'https://www.mini4k.com/shows/',
        mini4kDownloadDetailUrlPrefix = 'https://www.mini4k.com/torrents/';


    const ICON = {
        QBITTORRENT: '<svg t="1743203971711" style="width: 20px;height: 20px;" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3524" width="32" height="32"><path d="M730.091219 361.068278c-28.999869 0-54.068954 12.661259-75.202085 37.973432-21.143475 25.074258-31.71004 61.449517-31.710041 109.130949 0 46.946996 6.387522 80.865516 19.172912 101.755558 20.890043 34.166779 49.155476 51.250169 84.791126 51.250169 29.005041 0 54.074126-12.537129 75.212429-37.606214 21.138303-25.317346 31.704868-62.92356 31.704869-112.818644 0-51.120867-10.199347-88.851212-30.598043-113.18069-20.155607-24.33465-44.609215-36.504561-73.365995-36.50456zM290.996899 358.859798c-28.756781 0-53.210389 12.288869-73.365996 36.866607-19.912519 24.33465-29.863607 62.42704-29.863606 114.287515 0 50.386431 10.566565 88.240906 31.704868 113.558252 21.138303 25.069086 46.455648 37.601042 75.946865 37.601042 28.265433 0 52.600083-11.916479 72.998777-35.759781 20.403867-24.08639 30.603214-60.585779 30.603215-109.498166 0-52.108735-10.814825-91.307951-32.444476-117.613165-21.386563-26.294869-46.574606-39.442304-75.579647-39.442304z" fill="#3C77C5" p-id="3525"></path><path d="M284.728334 306.502803c-30.96526 0-59.478953 8.114998-85.530734 24.33465-25.808694 15.976564-45.597083 39.571606-59.359995 70.790298-13.519825 31.21352-20.274565 66.481953-20.274565 105.810472 0 64.640692 16.711 115.518471 50.138171 152.628166 33.67026 37.114867 73.738386 55.6723 120.188862 55.6723 20.895215 0 40.554302-4.789349 58.987605-14.378391 18.681563-9.58387 33.184084-21.629651 43.507561-36.126999v191.713596h66.357824V315.357409H399.026194v52.346651c-27.774085-40.79739-65.871647-61.196085-114.292688-61.196085z m6.268565 52.356995c29.005041 0 54.193084 13.147435 75.579647 39.447476 21.629651 26.300041 32.444476 65.499258 32.444476 117.607993 0 48.912388-10.199347 85.411777-30.603215 109.498166-20.398695 23.843302-44.733345 35.764953-72.998777 35.764953-29.491217 0-54.808562-12.537129-75.946865-37.606214-21.138303-25.317346-31.704868-63.17182-31.704868-113.558252 0-51.860475 9.95626-89.952865 29.863606-114.287515 20.155607-24.577738 44.609215-36.866607 73.365996-36.866607zM562.345055 166.406594v540.482661h61.568474v-49.031346c26.300041 38.58891 62.55117 57.880779 108.763731 57.88078 46.450476 0 86.270342-18.309173 119.449254-54.927521 33.184084-36.623519 49.776126-88.489166 49.776126-155.586597 0-28.265433-3.930783-54.808562-11.797521-79.63456-7.866738-24.825998-18.805693-45.964301-32.816866-63.409736-13.762913-17.698868-31.456608-31.342822-53.086259-40.926691-21.386563-9.826957-44.117867-14.75078-68.209429-14.750781-43.502389 0-79.267342 17.57991-107.284515 52.724213v-192.81525z m167.751336 194.661684c28.756781 0 53.210389 12.169911 73.365995 36.50456 20.398695 24.329478 30.603214 62.059822 30.603215 113.18069 0 49.895083-10.571737 87.501298-31.704868 112.818644-21.143475 25.069086-46.21256 37.601042-75.217602 37.601042-35.635651 0-63.901084-17.078218-84.791126-51.244997-12.785389-20.895215-19.172911-54.808562-19.172912-101.755558 0-47.681432 10.566565-84.056691 31.704868-109.130949 21.138303-25.312174 46.207388-37.973432 75.21243-37.973432z" fill="#F2F2F2" p-id="3526"></path><path d="M512.036205 42.654167A468.254523 468.254523 0 0 0 43.792026 510.908691 468.254523 468.254523 0 0 0 512.036205 979.168386a468.254523 468.254523 0 0 0 468.254523-468.259695A468.254523 468.254523 0 0 0 512.041377 42.654167z m50.30885 123.752427h66.362995v192.820422c28.017173-35.144303 63.782126-52.719041 107.284515-52.719041 24.091562 0 46.822866 4.913479 68.209429 14.745609 21.629651 9.58387 39.323346 23.222652 53.086259 40.921519 14.011173 17.450608 24.950128 38.58891 32.816866 63.409736 7.861566 24.825998 11.792349 51.374299 11.792348 79.639732 0 67.097431-16.58687 118.957906-49.770953 155.581425-33.178912 36.623519-72.998778 54.932692-119.449254 54.932693-46.21256 0-82.463689-19.291869-108.763731-57.875608v49.031346h-61.568474zM284.728334 306.502803c48.42104 0 86.518602 20.403867 114.292688 61.201257V315.357409h59.727213v541.589486H392.385239V665.233299c-10.323477 14.497348-24.825998 26.543129-43.507561 36.132171-18.433303 9.58387-38.09239 14.37839-58.987605 14.378391-46.450476 0-86.518602-18.562605-120.188862-55.6723-33.427172-37.114867-50.143343-87.992646-50.143343-152.633338 0-39.328518 6.759912-74.596951 20.279737-105.810472 13.762913-31.218692 33.551302-54.813734 59.359995-70.790298 26.051781-16.219652 54.565474-24.329478 85.530734-24.329478z" fill="#3C77C5" p-id="3527"></path><path d="M512 0A512 512 0 0 0 0 512a512 512 0 0 0 512 512 512 512 0 0 0 512-512A512 512 0 0 0 512 0zM512.036205 42.654167a468.254523 468.254523 0 0 1 468.254523 468.254524 468.254523 468.254523 0 0 1-468.254523 468.259695A468.254523 468.254523 0 0 1 43.786854 510.908691 468.254523 468.254523 0 0 1 512.036205 42.654167z" fill="#B8CDEB" p-id="3528"></path></svg>',
        SETTING: '<svg t="1743508812925" style="width: 20px;height: 20px;" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7008" width="64" height="64"><path d="M924.8 625.7l-65.5-56c3.1-19 4.7-38.4 4.7-57.8s-1.6-38.8-4.7-57.8l65.5-56c10.1-8.6 13.8-22.6 9.3-35.2l-0.9-2.6c-18.1-50.5-44.9-96.9-79.7-137.9l-1.8-2.1c-8.6-10.1-22.5-13.9-35.1-9.5l-81.3 28.9c-30-24.6-63.5-44-99.7-57.6l-15.7-85c-2.4-13.1-12.7-23.3-25.8-25.7l-2.7-0.5c-52.1-9.4-106.9-9.4-159 0l-2.7 0.5c-13.1 2.4-23.4 12.6-25.8 25.7l-15.8 85.4c-35.9 13.6-69.2 32.9-99 57.4l-81.9-29.1c-12.5-4.4-26.5-0.7-35.1 9.5l-1.8 2.1c-34.8 41.1-61.6 87.5-79.7 137.9l-0.9 2.6c-4.5 12.5-0.8 26.5 9.3 35.2l66.3 56.6c-3.1 18.8-4.6 38-4.6 57.1 0 19.2 1.5 38.4 4.6 57.1L99 625.5c-10.1 8.6-13.8 22.6-9.3 35.2l0.9 2.6c18.1 50.4 44.9 96.9 79.7 137.9l1.8 2.1c8.6 10.1 22.5 13.9 35.1 9.5l81.9-29.1c29.8 24.5 63.1 43.9 99 57.4l15.8 85.4c2.4 13.1 12.7 23.3 25.8 25.7l2.7 0.5c26.1 4.7 52.8 7.1 79.5 7.1 26.7 0 53.5-2.4 79.5-7.1l2.7-0.5c13.1-2.4 23.4-12.6 25.8-25.7l15.7-85c36.2-13.6 69.7-32.9 99.7-57.6l81.3 28.9c12.5 4.4 26.5 0.7 35.1-9.5l1.8-2.1c34.8-41.1 61.6-87.5 79.7-137.9l0.9-2.6c4.5-12.3 0.8-26.3-9.3-35zM788.3 465.9c2.5 15.1 3.8 30.6 3.8 46.1s-1.3 31-3.8 46.1l-6.6 40.1 74.7 63.9c-11.3 26.1-25.6 50.7-42.6 73.6L721 702.8l-31.4 25.8c-23.9 19.6-50.5 35-79.3 45.8l-38.1 14.3-17.9 97c-28.1 3.2-56.8 3.2-85 0l-17.9-97.2-37.8-14.5c-28.5-10.8-55-26.2-78.7-45.7l-31.4-25.9-93.4 33.2c-17-22.9-31.2-47.6-42.6-73.6l75.5-64.5-6.5-40c-2.4-14.9-3.7-30.3-3.7-45.5 0-15.3 1.2-30.6 3.7-45.5l6.5-40-75.5-64.5c11.3-26.1 25.6-50.7 42.6-73.6l93.4 33.2 31.4-25.9c23.7-19.5 50.2-34.9 78.7-45.7l37.9-14.3 17.9-97.2c28.1-3.2 56.8-3.2 85 0l17.9 97 38.1 14.3c28.7 10.8 55.4 26.2 79.3 45.8l31.4 25.8 92.8-32.9c17 22.9 31.2 47.6 42.6 73.6L781.8 426l6.5 39.9zM512 326c-97.2 0-176 78.8-176 176s78.8 176 176 176 176-78.8 176-176-78.8-176-176-176z m79.2 255.2C570 602.3 541.9 614 512 614c-29.9 0-58-11.7-79.2-32.8C411.7 560 400 531.9 400 502c0-29.9 11.7-58 32.8-79.2C454 401.6 482.1 390 512 390c29.9 0 58 11.6 79.2 32.8C612.3 444 624 472.1 624 502c0 29.9-11.7 58-32.8 79.2z" fill="#1296DB" p-id="7009"></path></svg>',
        SUCCESS: '<svg t="1743238268601" style="width: 20px;height: 20px;" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5483" width="32" height="32"><path d="M512 512m-512 0a512 512 0 1 0 1024 0 512 512 0 1 0-1024 0Z" fill="#48D19C" p-id="5484"></path><path d="M232.448 558.592a132.973714 132.973714 0 0 1-5.412571-6.436571 73.362286 73.362286 0 0 1-7.094858-10.678858 47.689143 47.689143 0 0 1-4.461714-11.702857 36.790857 36.790857 0 0 1 0.731429-19.017143 47.835429 47.835429 0 0 1 5.193143-11.410285 84.114286 84.114286 0 0 1 7.826285-10.24c2.194286-2.340571 4.388571-4.681143 6.802286-6.875429l5.558857-5.193143a77.677714 77.677714 0 0 1 18.066286-13.312 47.835429 47.835429 0 0 1 11.702857-4.534857 36.864 36.864 0 0 1 19.017143 0.731429c4.388571 1.316571 8.265143 3.291429 11.410286 5.266285 3.657143 2.267429 7.021714 4.973714 10.166857 7.753143 2.340571 2.194286 4.681143 4.388571 6.875428 6.729143l-0.073143 0.146286 123.904 135.314286 200.850286-206.701715c4.900571-4.827429 9.947429-9.581714 15.140572-14.116571a157.257143 157.257143 0 0 1 22.162285-16.091429c6.729143-4.096 15.067429-8.118857 24.576-10.825143a78.848 78.848 0 0 1 40.740572-0.512c9.581714 2.486857 17.993143 6.363429 24.868571 10.24 8.118857 4.534857 15.579429 9.947429 22.528 15.579429 5.339429 4.388571 10.459429 8.996571 15.506286 13.750857L727.259429 490.203429 525.165714 698.148571l0.146286 0.146286a294.765714 294.765714 0 0 1-15.36 13.970286 157.257143 157.257143 0 0 1-22.235429 15.872 102.180571 102.180571 0 0 1-24.722285 10.459428 78.848 78.848 0 0 1-40.813715 0 102.253714 102.253714 0 0 1-24.722285-10.459428 157.257143 157.257143 0 0 1-22.308572-15.872 294.765714 294.765714 0 0 1-15.36-13.897143l0.292572-0.292571L232.301714 558.811429l0.146286-0.146286z" fill="#FFFFFF" p-id="5485" data-spm-anchor-id="a313x.search_index.0.i2.73623a81hxtkXa" class="selected"></path></svg>',
        FAIL: '<svg t="1743238418205" style="w idth: 20px;height: 20px;" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7540" width="32" height="32"><path d="M512 1024C229.222 1024 0 794.778 0 512S229.222 0 512 0s512 229.222 512 512-229.222 512-512 512z m-48.384-808.755l17.92 364.851h60.928l17.92-364.851h-96.768z m88.166 418.611c-10.513-10.035-23.893-15.053-40.14-15.053-15.77 0-28.672 5.018-38.708 15.053-10.99 9.557-16.486 22.46-16.486 38.707 0 16.248 5.495 29.15 16.486 38.707 10.513 10.513 23.416 15.77 38.708 15.77 15.291 0 28.672-5.018 40.14-15.053 10.513-10.513 15.77-23.654 15.77-39.424 0-15.292-5.257-28.194-15.77-38.707z" fill="#d81e06" p-id="7541"></path></svg>'
    }


    const QBITTORRENT_URL = {
        login: '/api/v2/auth/login',
        /**
         * 获取 API 版本
         */
        webapiversion: '/api/v2/app/webapiVersion',
        /**
         * 获取生成信息
         */
        buildInfo: '/api/v2/app/buildInfo',
        /**
         * 添加链接或种子信息
         */
        addTorrent: '/api/v2/torrents/add',
        /**
         * 获取配置
         */
        perferences: '/api/v2/app/preferences'
    }

    const QBITTORRENT_CONFIG_STORAGE_KEY = {
        QBITTORRENT_CONFIG_URL: 'QBITTORRENT_CONFIG_URL',
        QBITTORRENT_CONFIG_USERNAME: 'QBITTORRENT_CONFIG_USERNAME',
        QBITTORRENT_CONFIG_PASSWORD: 'QBITTORRENT_CONFIG_PASSWORD'
    };

    const qBittorrentConfig = {
        url: '',
        username: '',
        password: '',
        SID: '',
        savePath: ''
    }


    //判断当前页面是影视详情页还是资源详情页
    if (window.location.href.startsWith(mini4kMovieDetailUrlPrefix) || window.location.href.startsWith(mini4kTVDetailUrlPrefix)) {
        mini4kDetail();
    } else if (window.location.href.startsWith(mini4kDownloadDetailUrlPrefix)) {
        mini4kDownloadDetail();
    } else {

    }

    /**
     * 影视详情页处理
     */
    function mini4kDetail() {
        //搜索磁力链接
        let magnetLinks = $('div.magnet-link');
        if (magnetLinks && magnetLinks.length > 0) {
            for (let index = 0; index < magnetLinks.length; index++) {
                var link = $(magnetLinks[index]), magnetLink = link.attr('data-link');
                var qBittorrent = $('<div class="magnet-link qbittorrent">' + ICON.QBITTORRENT + '</div>');
                qBittorrent.attr('data-url', magnetLink);
                qBittorrent.on('click', function (event) {
                    addLinkToQBittorrent(event);
                });
                link.after(qBittorrent);
            }
        }
        //搜索种子文件连接
        let torrentHrefs = $('i.download');
        if (torrentHrefs && torrentHrefs.length > 0) {
            for (let index = 0; index < torrentHrefs.length; index++) {
                var link = $(torrentHrefs[index]).parent(), magnetLink = MINI4K_DOMAIN + link.attr('href');
                if (link.hasClass('link-node')) {
                    continue;
                }
                var qBittorrent = $('<div class="magnet-link qbittorrent">' + ICON.QBITTORRENT + '</div>');
                qBittorrent.attr('data-url', magnetLink);
                qBittorrent.on('click', function (event) {
                    addLinkToQBittorrent(event);
                });
                link.after(qBittorrent);
            }
        }
    }

    /**
     * 影视下载详情页处理
     */
    function mini4kDownloadDetail() {

    }

    function addLinkToQBittorrent(event) {
        let $element = $(event.target).parents('.qbittorrent');
        let magnetLink = $element.attr('data-url');
        QBITTORRENT_CLIENT.addTorrent(magnetLink, $element);
    }


    const DIALOG_CONTEXT = {
        init: function () {
            /*qBittorrentConfig dialog模板*/
            let qBittorrentConfigModel = $(
                `
                <div id="qBittorrentConfigModal" style="display: none;" title="配置QBittorrent服务端信息">
                    <form autocomplete="off">
                        <div class="js-form-item form-item js-form-type-textfield form-item-name js-form-item-name">
                            <label for="ServerUrl" class="js-form-required form-required">qBittorrent Server Url</label>
                            <input autocapitalize="off" spellcheck="false" autocomplete="off"  placeholder="例如：http://localhost:8085"
                                 type="text" id="ServerUrl" name="ServerUrl" value=""
                                 size="60" class="form-text required" required="required" aria-required="true">
                        </div>

                        <div class="js-form-item form-item js-form-type-textfield form-item-name js-form-item-name">
                            <label for="ServerLoginInput" class="js-form-required form-required">qBittorrent登录名</label>
                            <input autocapitalize="off" spellcheck="false" autocomplete="off" placeholder="请输入QBittorrent登录账号"
                                type="text" id="ServerLoginInput" name="ServerLoginInput" value="" size="60"
                                class="form-text required" required="required" aria-required="true">
                        </div>

                        <div class="js-form-item form-item js-form-type-textfield form-item-name js-form-item-name">
                            <label for="ServerPasswdInput" class="js-form-required form-required">qBittorrent登录密码</label>
                            <input autocapitalize="off" spellcheck="false" autocomplete="off" placeholder="请输入QBittorrent登录密码"
                                type="password" id="ServerPasswdInput" name="ServerPasswdInput" value="" size="60"
                                class="form-text required" required="required" aria-required="true">
                        </div>
                    </form>
                </div>
                `
            );
            $('body').append(qBittorrentConfigModel);
            /*qBittorrentConfig clear dialog模板*/
            let qBittorrentConfigCleanBtn = $(
                `
                <div id="qBittorrentConfigCleanBtn" style="position: fixed;top: 60px;left: 10px;" title="清楚QBittorrent服务端信息">
                    ` + ICON.SETTING + `
                </div>
                `
            );
            $('body').append(qBittorrentConfigCleanBtn);
            $('#qBittorrentConfigCleanBtn').on('click', function (event) {
                if (confirm('确定要清除qBittorrent的配置信息么？')) {
                    localStorage.removeItem(QBITTORRENT_CONFIG_STORAGE_KEY.QBITTORRENT_CONFIG_URL);
                    localStorage.removeItem(QBITTORRENT_CONFIG_STORAGE_KEY.QBITTORRENT_CONFIG_USERNAME);
                    localStorage.removeItem(QBITTORRENT_CONFIG_STORAGE_KEY.QBITTORRENT_CONFIG_PASSWORD);
                }
            });
        },
        openQBittorrentConfigDialog: function () {
            $('#qBittorrentConfigModal').show();
            $('#qBittorrentConfigModal').dialog({
                resizable: false,
                width: 520,
                height: 'auto',
                modal: true,
                buttons: {
                    "确认": function () {
                        let serverUrl = $('#ServerUrl').val(), loginName = $('#ServerLoginInput').val(),
                            loginPwd = $('#ServerPasswdInput').val();
                        localStorage.setItem(QBITTORRENT_CONFIG_STORAGE_KEY.QBITTORRENT_CONFIG_URL, serverUrl);
                        localStorage.setItem(QBITTORRENT_CONFIG_STORAGE_KEY.QBITTORRENT_CONFIG_USERNAME, loginName);
                        localStorage.setItem(QBITTORRENT_CONFIG_STORAGE_KEY.QBITTORRENT_CONFIG_PASSWORD, loginPwd);
                        $(this).dialog('close');
                        $('#qBittorrentConfigModal').hide();
                        QBITTORRENT_CLIENT.login();
                    },
                    "取消": function () {
                        $(this).dialog('close');
                        $('#qBittorrentConfigModal').hide();
                    }
                }
            });
        },
        buildClearConfigDialog: function () {

        }
    }


    /**
     * QBittorrent客户端操作
     */
    const QBITTORRENT_CLIENT = {
        /**
         * 登录
         */
        login: function () {
            let cookie = '';
            GM_xmlhttpRequest({
                url: qBittorrentConfig.url + QBITTORRENT_URL.login,
                method: 'POST',
                data: 'username=' + qBittorrentConfig.username + '&password=' + qBittorrentConfig.password,
                anonymous: true,
                cookie: cookie,
                headers: {
                    'Content-type': ' application/x-www-form-urlencoded; charset=UTF-8',
                    'Referer': qBittorrentConfig.url
                },
                onload: function (res) {
                    let cookies = res.responseHeaders.match(/Set-Cookie:(.*?);/i);
                    if (cookies && cookies.length > 0) {
                        for (let i = 0; i < cookies.length; i++) {
                            let cookie = cookies[i];
                            if (cookie.startsWith('SID=')) {
                                qBittorrentConfig.SID = cookie.substring('SID='.length);
                                console.log("SID = " + qBittorrentConfig.SID);
                                QBITTORRENT_CLIENT.webApiVersion();
                                QBITTORRENT_CLIENT.buildInfo();
                                QBITTORRENT_CLIENT.getAppPerferences();
                            }
                        }
                        if (qBittorrentConfig.SID === '') {
                            alert('QBittorrent登录失败！');
                        }
                    }
                }
            });
        },
        webApiVersion: function () {
            let cookie = 'SID=' + qBittorrentConfig.SID;
            GM_xmlhttpRequest({
                url: qBittorrentConfig.url + QBITTORRENT_URL.webapiversion,
                method: 'POST',
                data: '',
                anonymous: true,
                cookie: cookie,
                headers: {
                    'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
                    'Referer': qBittorrentConfig.url
                },
                onload: function (res) {
                    console.log("webApiVersion=" + res.response);
                }
            });
        },
        buildInfo: function () {
            let cookie = 'SID=' + qBittorrentConfig.SID;
            GM_xmlhttpRequest({
                url: qBittorrentConfig.url + QBITTORRENT_URL.buildInfo,
                method: 'POST',
                data: '',
                anonymous: true,
                cookie: cookie,
                headers: {
                    'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
                    'Referer': qBittorrentConfig.url
                },
                onload: function (res) {
                    console.log("buildInfo=" + res.response);
                }
            });
        },
        addTorrent: function (url, $element) {
            let cookie = 'SID=' + qBittorrentConfig.SID;
            let formData = new FormData();
            formData.set('urls', url);
            formData.set('savepath', qBittorrentConfig.savePath);
            formData.set('autoTMM', false);
            formData.set('cookie', '');
            formData.set('rename', '');
            formData.set('category', '');
            formData.set('stopped', false);
            formData.set('stopCondition', 'None');
            formData.set('contentLayout', 'Original');
            GM_xmlhttpRequest({
                url: qBittorrentConfig.url + QBITTORRENT_URL.addTorrent,
                method: 'POST',
                data: formData,
                anonymous: true,
                cookie: cookie,
                contentType: false,
                onload: function (res) {
                    if (res.response !== 'Ok.') {
                        $element.html(ICON.FAIL);
                        alert('添加到QBittorrent出错！');
                        return;
                    } else {
                        $element.html(ICON.SUCCESS);
                    }
                },
                onerror: function (res) {
                    console.error(res);
                }
            });
        },
        getAppPerferences: function () {
            let cookie = 'SID=' + qBittorrentConfig.SID;
            GM_xmlhttpRequest({
                url: qBittorrentConfig.url + QBITTORRENT_URL.perferences,
                method: 'GET',
                data: '',
                anonymous: true,
                cookie: cookie,
                headers: {
                    'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
                    'Referer': qBittorrentConfig.url
                },
                onload: function (res) {
                    let perferences = JSON.parse(res.response);
                    qBittorrentConfig.savePath = perferences.save_path;
                }
            });
        },
        init: function () {
            qBittorrentConfig.url = localStorage.getItem(QBITTORRENT_CONFIG_STORAGE_KEY.QBITTORRENT_CONFIG_URL);
            qBittorrentConfig.username = localStorage.getItem(QBITTORRENT_CONFIG_STORAGE_KEY.QBITTORRENT_CONFIG_USERNAME);
            qBittorrentConfig.password = localStorage.getItem(QBITTORRENT_CONFIG_STORAGE_KEY.QBITTORRENT_CONFIG_PASSWORD);

            DIALOG_CONTEXT.init();
            console.log('qBittorrentConfig.url=' + qBittorrentConfig.url);
            if (qBittorrentConfig.url === null) {
                DIALOG_CONTEXT.openQBittorrentConfigDialog();
            } else {
                QBITTORRENT_CLIENT.login();
            }
        }
    }
    QBITTORRENT_CLIENT.init();

})();